﻿using FairyGUI;
using System;

/// <summary>
/// 主界面UI
/// </summary>
public class UIHomeWindow : UIBaseWindow {
    UIHeadpicImage _headpicImg;
    /// <summary>
    /// 昵称
    /// </summary>
    GTextField _name;
    /// <summary>
    /// 用户编号
    /// </summary>
    GTextField _uid;
    /// <summary>
    /// 房卡
    /// </summary>
    GTextField _card;

    /// <summary>
    /// 公告标题
    /// </summary>
    GTextField _noticeTitle;
    /// <summary>
    /// 公告内容
    /// </summary>
    GTextField _noticeText;

    /// <summary>
    /// 消息组件文本
    /// </summary>
    GTextField _annText;
    /// <summary>
    /// 消息组件动画
    /// </summary>
    Transition _annAnim;
    /// <summary>
    /// 已经关闭
    /// </summary>
    bool _closed = false;

    public UIHomeWindow()
    {
        UIPackage.AddPackage("UI/通用组件");
        UIPackage.AddPackage("UI/主界面");
        UIPackage.AddPackage("UI/大厅");

        base.Init(UIPackage.CreateObject("主界面", "主界面").asCom);

        // 用户信息初始化
        GComponent _userInfoComp = uiComp.GetChild("用户信息").asCom;
        _name = _userInfoComp.GetChild("昵称").asTextField;
        _uid = _userInfoComp.GetChild("用户编号").asTextField;
        _card = _userInfoComp.GetChild("房卡数量").asTextField;
        GButton addCardBtn = _userInfoComp.GetChild("充值房卡按钮").asButton;
        addCardBtn.onClick.Add(() => {
            DebugInfo.Log("添加房卡按钮被点击");

            AudioController.inst.PlayUIClickEffect();
            // TODO 添加房卡按钮被点击
        });

        _headpicImg = new UIHeadpicImage(_userInfoComp.GetChild("头像").asCom, 
            ()=> {
                DebugInfo.Log("头像按钮被点击");

                AudioController.inst.PlayUIClickEffect();
                UIUserInfoWindow.Popup();
            }
       );

        // 游戏公告初始化
        GComponent _noticeComp = uiComp.GetChild("游戏公告").asCom;
        _noticeTitle = _noticeComp.GetChild("公告标题").asTextField;
        _noticeText = _noticeComp.GetChild("公告内容").asTextField;

        // 滚动消息初始化
        GComponent _annComp = uiComp.GetChild("消息组件").asCom;
        GComponent _annTextComp = _annComp.GetChild("消息").asCom;
        _annText = _annTextComp.GetChild("滚动消息").asTextField;
        _annAnim = _annTextComp.GetTransition("消息动画");

        // 主按钮初始化
        GComponent _mainBtnComp = uiComp.GetChild("主按钮").asCom;
        GButton createBtn = _mainBtnComp.GetChild("创建房间按钮").asButton;
        createBtn.onClick.Add(() => {
            DebugInfo.Log("创建房间按钮被点击");

            AudioController.inst.PlayUIClickEffect();
            UICreateRoomWindow.Popup();
        });
        GButton joinBtn = _mainBtnComp.GetChild("加入房间按钮").asButton;
        joinBtn.onClick.Add(()=> {
            DebugInfo.Log("加入房间按钮被点击");

            AudioController.inst.PlayUIClickEffect();
            UIJoinRoomWindow.Popup();
        });

        // 按钮组初始化
        GComponent _btnGrpComp = uiComp.GetChild("按钮组").asCom;
        GButton mailBtn = _btnGrpComp.GetChild("消息按钮").asButton;
        mailBtn.onClick.Add(()=> {
            DebugInfo.Log("邮件按钮被点击");

            AudioController.inst.PlayUIClickEffect();
            UIMessageWindow.Popup();
        });
        GButton reportBtn = _btnGrpComp.GetChild("战绩按钮").asButton;
        reportBtn.onClick.Add(() => {
            DebugInfo.Log("战报按钮被点击");

            AudioController.inst.PlayUIClickEffect();
            // TODO 战报按钮被点击
        });
        GButton helpBtn = _btnGrpComp.GetChild("帮助按钮").asButton;
        helpBtn.onClick.Add(() => {
            DebugInfo.Log("帮助按钮被点击");

            AudioController.inst.PlayUIClickEffect();
            // TODO 帮助按钮被点击
        });
        GButton setBtn = _btnGrpComp.GetChild("设置按钮").asButton;
        setBtn.onClick.Add(() => {
            DebugInfo.Log("设置按钮被点击");

            AudioController.inst.PlayUIClickEffect();
            UISettingWindow.Popup();
        });

        ShowAnn();
    }

    public override void OnExit()
    {
        uiComp.Dispose();
        _closed = true;

        UIPackage.RemovePackage("UI/大厅");
        UIPackage.RemovePackage("UI/主界面");
        UIPackage.RemovePackage("UI/通用组件");        
    }

    /// <summary>
    /// 更新用户信息
    /// </summary>
    public void UpdateUserInfo()
    {
        UserInfo userInfo = DataKernel.inst.userInfo;

        _headpicImg.SetHeadpic(userInfo.headpic);
        _name.text = userInfo.name;
        _uid.text = "ID:" + userInfo.uid;
        _card.text = "" + userInfo.card;
    }

    /// <summary>
    /// 更新公告
    /// </summary>
    /// <param name="title"></param>
    /// <param name="content"></param>
    public void UpdateNotice(string title, string content)
    {
        _noticeTitle.text = title;
        _noticeText.text = content;
    }

    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="ann">消息文本</param>
    /// <param name="callback">显示完成回调</param>
    void ShowAnn()
    {
        if (!_closed)
        {
            // 获取随机滚动消息
            _annText.text = DataKernel.inst.annInfo.getAnnRandom();
            // 播放消息动画
            _annAnim.Play();
            // 继续播放
            TimerController.inst.CallAfter(12.0f, param => {
                ShowAnn();
            });
        }
    }
}
